<?php
/*
 * 		author GuoLong
 * 		2014/11/17
 * 		email:guolong@howdo.cc
 */
class FeedbackController extends Controller {

	private $rootPath = "";		///var/www/html/howdo/
	private $targetPath = "common/upload/";
	private $uploadpath;
	private $send_mes;
	public function __construct() {
		parent::__construct ();
		$this->view->web_host = $this->getRequest ()->hostUrl;
		$this->view->web_app_url = $this->getRequest ()->hostUrl . "/clientapi.php";
		$this->uploadpath=$this->rootPath.$this->targetPath;
		$this->send_mes=$this->getRequest()->hostUrl. "/common/clientapi/images/50.png";
	}
	//删除提问
// 	http://localhost/xdfServer/clientapi1.php/feedback/DeleteMyQuestion/qa_id/1
	public function DeleteMyQuestion(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		$qa_id=$this->getRequest()->get("qa_id");
		$qa=new questionoranswer();
		if($qa->delMyQA($qa_id, $user_id)){
			$msg.="删除提问成功！";
			$this->view->setstate("1");
		}else{
			$msg.="删除提问失败！";
			$this->view->setstate("0");
		}
		$this->view->setMsg($msg);
		$data=null;
		$this->view->setData($data);
		$this->view->display("json");
	}
	//学生提问和老师回答的接口，
// 	http://localhost/xdfServer/clientapi1.php/feedback/questionoranswer/video_id/2/target_id/0/content_type/0/content/afjkasdfsdf
	public function QuestionOrAnswer(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		$user_role_id=$userinfo['role_id'];
		
		$video_id=$this->getRequest()->get("video_id");
		$target_id=$this->getRequest()->get("target_id");
		$content_type=$this->getRequest()->get("content_type");
		$content=$this->getRequest()->get("content");
		//上传content同时保存$content_id
		$content_id=0;
		$data = null;
		switch ($content_type){
			case 0://文字f
				$text=new text();
				$content_id=$text->addText($content);
				break;
			case 1://语音
				//加载上传类
				$time=$content;
				$this->getApp()->loadUtilClass("UploadUtil");
				//设置上传类的参数（参数为数组）
				$upPara=array(
					'allowType'=>array("acc","mp3","wav","amr","*"),
					'maxsize'=>"200000000",
					'israndname'=>true,
					'filepath'=>$this->uploadpath."audio/"
				);
				$up=new UploadUtil($upPara);
				if($up->uploadFile("audio")){
					//$msg.="上传音频成功！";
					$audio=new audio();
					$content_id=$audio->addAudio($up->getNewFileName(),$time);
					if($content_id){
						//$msg.="保存音频到数据库成功！";
						$msg.="上传音频成功！";
						$this->view->setState("1");
					}else{
						//$msg.="保存音频到数据库失败!";
						$msg.="上传音频失败！";
						$this->view->setState("0");
					}
				}else{
					//$msg.="上传音频失败！".$up->getErrorMsg();
					$msg.="上传音频失败！";
					$this->view->setState("0");
				}
				break;
			//
			case 2:
				//获取视频相关参数
				$title=$this->getRequest()->get("title");
				$time=$content;
				$create_time=$this->getRequest()->get("create_time");
				$video_name=$this->getRequest()->get("video_name");
				$video=new video();
				$content_id=$video->addVideo($user_id,$title,$video_name,VideoTypeEnum::VIDEO_QA,$time,$create_time);
				break;
			default:
				break;
		}
		if(!$content_id){
			$this->view->setMsg($msg);
			$data=null;
			$this->view->setState("0");
			$this->view->setData($data);
			$this->view->display("json");
			exit;
		}
		//保存提问或问题到数据库
		$qa=new questionoranswer();
		if($qa_id=$qa->addQA($user_id, $target_id, $video_id, $content_type, $content_id)){
			//$msg.="保存提问或问题成功！";
			$msg.="成功喽！";
			//在此，设置
			//message 
			$question_answer = new Questionoranswer();
			$video_list = new video();
			$teacher_id = $video_list->getVideoNameById($video_id);
			
			if($target_id){
				$receiver = $question_answer->getUserId($target_id);
			}else{
				//如果无回复对象，则将该信息发给视频创建者
				$receiver['user_id'] = $teacher_id['user_id'];
			}			
			$user=new user();
			$receiver_info=$user->getUserInfo($receiver["user_id"]);
			$receiver_role=$receiver_info["role_id"];
			//发送消息
			$message = new message();
			$video=new video();
			$video_info = $video->getVideoById($video_id);
			$video_type = $video_info['type'];
			$data['user_role_id']=$user_role_id;
			$data['receiver_role']=$receiver_role;
			if($user_role_id == RoleCodeEnum::ROLE_TEACHER){
				//教师给学生发送
				if($receiver['user_id'] <> $user_id){
					if($video_type == VideoTypeEnum::VIDEO_ITEM)
						$message->addMessage(MessageCodeEnum::MESSAGE_STU_REPLY_ITEM,$video_id,
								MessageCodeEnum::getCodes(MessageCodeEnum::MESSAGE_STU_REPLY_ITEM)
								,$user_id,$receiver['user_id']);
					else if($video_type == VideoTypeEnum::VIDEO_PERIOD)
						$message->addMessage(MessageCodeEnum::MESSAGE_STU_REPLY_PERIOD,$video_id,
								MessageCodeEnum::getCodes(MessageCodeEnum::MESSAGE_STU_REPLY_PERIOD)
								,$user_id,$receiver['user_id']);
				}
			}else if($user_role_id == RoleCodeEnum::ROLE_STUDENT){
				//学生提问
				if($receiver_role == RoleCodeEnum::ROLE_TEACHER ){//学生给老师提问
					$video_info  ==  $video->getVideoById($video_id);
					if($video_type == VideoTypeEnum::VIDEO_ITEM)
						$message->addMessage(MessageCodeEnum::MESSAGE_TEA_REPLY_ITEM,$video_id,
							MessageCodeEnum::getCodes(MessageCodeEnum::MESSAGE_TEA_REPLY_ITEM)
								,$user_id,$receiver['user_id']);
					else if($video_type == VideoTypeEnum::VIDEO_PERIOD)
						$message->addMessage(MessageCodeEnum::MESSAGE_TEA_REPLY_PERIOD,$video_id,
							MessageCodeEnum::getCodes(MessageCodeEnum::MESSAGE_TEA_REPLY_PERIOD)
								,$user_id,$receiver['user_id']);
				} else {//学生给学生提问
					$video_info  ==  $video->getVideoById($video_id);
					if($video_type == VideoTypeEnum::VIDEO_ITEM)
						$message->addMessage(MessageCodeEnum::MESSAGE_STU_REPLY_ITEM,$video_id,
							MessageCodeEnum::getCodes(MessageCodeEnum::MESSAGE_STU_REPLY_ITEM)
								,$user_id,$receiver['user_id']);
					else if($video_type == VideoTypeEnum::VIDEO_PERIOD)
						$message->addMessage(MessageCodeEnum::MESSAGE_STU_REPLY_PERIOD,$video_id,
							MessageCodeEnum::getCodes(MessageCodeEnum::MESSAGE_STU_REPLY_PERIOD)
								,$user_id,$receiver['user_id']);
				}
			}
			//透传
			$user = new user();
			$phone_info = $user->getPhoneInfo($receiver['user_id']);
			if ($phone_info['clientid']){
				$target = array(
						"type"=>$phone_info['type'],
						"ClientID"=>$phone_info['clientid'],
						"DeviceToken"=>$phone_info['devicetoken'],
				);
				$content = array(
						"info"=>"您有新回复了",//透传
						"title"=>'UDO学生端',
						"text"=>"您有新回复了",
						"logo_url"=>$this->send_mes,
				);
				$this->push->pushToSingle($target, $content);
			}
			
			
			//返回提交的问答详情
			$qa=new questionoranswer();
			$data=$qa->getQAInfo($qa_id);
			if($data){
				switch($data["content_type"]){
					case '0'://文字
						$text=new text();
						$content=$text->getTextById($data["content_id"]);
						$data["content"]=$content["content"];
						break;
					case '1'://音频
						$audio=new audio();
						$content=$audio->getAudioById($data["content_id"]);
						$data["content"]=$content["time"];
						$data["file_url"]=QiniuConfig::QINIU_PATH_AUDIO.$content["name"];
						break;
					case '2'://视频
						break;
					default:
						break;
				}
			}
			$this->view->setstate("1");
		}else{ 
			//$msg.="保存提问或问题失败！";
			$msg .= "出错了哟";
			$this->view->setstate("0");
		}

		$data["role_id"]=$user_role_id;
		$this->view->setMsg($msg);
		$this->view->setData($data);
		$this->view->display("json");
	}
	//评论视频
// 	http://localhost/xdfServer/clientapi1.php/feedback/comment/video_id/1/content/afjkasdfsdf
	public function Comment(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		$video_id=$this->getRequest()->get("video_id");
		$content=$this->getRequest()->get("content");
		$comment=new comment();
		if($comment->addComment($user_id, $video_id, $content)){
			//$msg.="保存评论成功！";
			$msg.="评论成功！";
			$this->view->setstate("1");
		}else{
			//$msg.="保存评论失败！";
			$msg.="评论失败！";
			$this->view->setstate("0");
		}
		
		$this->view->setMsg($msg);
		$data=null;
		$this->view->setData($data);
		$this->view->display("json");
		
	}
	//顶踩视频
// 	http://localhost/xdfServer/clientapi1.php/feedback/LikeVideo/video_id/1/like_num/2/dislike_num/3
	public function LikeVideo(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		$video_id=$this->getRequest()->get("video_id");
		$like=$this->getRequest()->get("like");
		//检查用户已经点击过的次数
		$video=new video();
		$result=$video->getPersonFavorNum($user_id, $video_id);
		$have_like_num=$result["like_num"];
		$have_dislike_num=$result["dislike_num"];

		$data=null;
		$can_like_flag=true;
		/*
		 * like :
		 * 1：顶视频；2：踩视频
		 */
			
		if($like=="2"){
			if($have_dislike_num > 4){
				$this->view->setMsg("已经踩过5次！");
				$this->view->setstate("-1");
				$can_like_flag=false;
			}
			$like_num=0;
			$dislike_num=1;
		}else if($like=="1"){
			if($have_like_num > 4){
				$this->view->setMsg("已经顶过5次！");
				$this->view->setstate("-2");
				$can_like_flag=false;
			}
			$like_num=1;
			$dislike_num=0;
		}else{
				$this->view->setMsg("参数错误！");
				$this->view->setstate("-3");
				$can_like_flag=false;
		}
		if($can_like_flag){
				//保存数据库
				$video=new video();
				if($video->likeVideo($user_id, $video_id, $like_num, $dislike_num)){
					$this->view->setMsg("顶踩成功！");
					$this->view->setstate("1");
				}else{
					$this->view->setMsg("顶踩失败！");
					$this->view->setstate("0");
				}
		}
		$video_info=$video->getVideoInfoById($video_id);
		$data["like_num"]=$video_info["like_num"];
		$data["dislike_num"]=$video_info["dislike_num"];
		$this->view->setData($data);
		$this->view->display("json");
	}
//顶踩视频
// 	http://localhost/xdfServer/clientapi1.php/feedback/LikeVideo/video_id/1/like_num/2/dislike_num/3
	public function LikeVideoOld(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		$video_id=$this->getRequest()->get("video_id");
		$like_num=$this->getRequest()->get("like_num");
		$dislike_num=$this->getRequest()->get("dislike_num");
		//参数检验
		if($like_num >5 || $dislike_num>5){
			$this->view->setMsg("顶/踩数 超过上限！");
			$this->view->setstate("-2");
			$data=null;
			$this->view->setData($data);
			$this->view->display("json");
			exit;
		}
		//检查用户已经点击过的次数
		$video=new video();
		$result=$video->getPersonFavorNum($user_id, $video_id);
		$have_like_num=$result["like_num"];
		$have_dislike_num=$result["dislike_num"];
		if($like_num+$have_like_num>5 ||$dislike_num+$have_dislike_num>5){//顶/踩次数已达到上限,返回状态-1
			$this->view->setstate("-1");
			$msg.="顶踩次数超过5次了";
		}else{
			if($video->likeVideo($user_id, $video_id, $like_num, $dislike_num)){
				//顶/踩成功返回1
				$this->view->setstate("1");
				$msg.="顶/踩成功！";
			}else{//顶/踩失败返回0
				$this->view->setstate("0");
				$msg.="顶/踩失败！";
			}
		}
		$data=null;
		$this->view->setMsg($msg);
		$this->view->setData($data);
		$this->view->display("json");
	}
	
	//给评论点赞
// 	http://localhost/xdfServer/clientapi1.php/feedback/Likecomment/comment_id/1/like/0
	public function LikeComment(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id = $userinfo ['id'];
		$comment_id=$this->getRequest()->get("comment_id");
		$like=$this->getRequest()->get("like");
		$comment=new comment();
		if($like=='1'){
			if(!$comment->islikeComment($comment_id,$user_id)){
				if($comment->likeComment($comment_id, $user_id)){
					$msg.="点赞成功！";
					$this->view->setState("1");
				}else{
					$msg.="点赞失败！$comment_id ,$user_id";
					$this->view->setState("0");
				}
			}else{
				$msg.="已经点赞！";
				$this->view->setState("0");
			}
		}else{
			if($comment->islikeComment($comment_id,$user_id)){
			if($comment->delLikeComment($comment_id, $user_id)){
				$msg.="取消点赞成功！";
				$this->view->setState("1");
			}else{
				$msg.="取消点赞失败！";
				$this->view->setState("0");
			}
			}else{
				$msg.="还未点赞！";
				$this->view->setState("0");
			}
		}

		$this->view->setStatus("1");
		$this->view->setMsg($msg);
		$this->view->display("json");		
	}
	//某用户获取id为video_id的评论列表
// 	http://localhost/xdfServer/clientapi1.php/feedback/getcommentlist/video_id/1/pagesize/10/timestamp/0
	public function GetCommentList(){
		
		$video_id=$this->getRequest()->get("video_id");
		$pagesize=$this->getRequest()->get("pagesize");
		$timestamp=$this->getRequest()->get("timestamp");
		$action=$this->getRequest()->get("action");
		$comment=new comment();
		$list=$comment->getCommentList($video_id,$timestamp,$pagesize,$action);
		if($list && count($list)<>0){
			$data=array("list"=>$list,"totalcount"=>count($list));
			$this->view->setMsg("获取列表成功！");
			$this->view->setState(1);
		}else{
			$data=null;
			$this->view->setMsg("没有更多了哟");
			$this->view->setState(0);
		}
		$this->view->setData($data);
		$this->view->setStatus("1");
		$this->view->display("json");
	}
	//某用户获取id为video_id的点赞数最多5个的评论
// 	http://localhost/xdfServer/clientapi1.php/feedback/getBestCommentList/video_id/1
	public function GetBestCommentList(){
		
		$video_id=$this->getRequest()->get("video_id");
		$comment=new comment();
		$list=$comment->getBestComment($video_id);
		if($list && count($list)<>0){
			$data=array("list"=>$list,"totalcount"=>count($list));
			$this->view->setMsg("获取最佳评论成功！");
			$this->view->setState(1);
		}else{
			$data=null;
			$this->view->setMsg("没有更多了哟");
			$this->view->setState(0);
		}
		$this->view->setData($data);
		$this->view->display("json");
	}

// 	教师端获取问答列表接口
	public function GetQAList(){
		return $this->stuGetQAList();
	}
//  学生端获取问答列表
	public function stuGetQAList(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id = $userinfo ['id'];
		//$user_id =18;
		$video_id=$this->getRequest()->get("video_id");
		$pagesize=$this->getRequest()->get("pagesize");
		$timestamp=$this->getRequest()->get("timestamp");
		$action=$this->getRequest()->get("action");
		$qa=new questionoranswer();
		$list=$qa->getQAList($video_id,$timestamp,$pagesize,$action);
		//error_reporting(0);
		//获取问答详情
		if($list && count($list)<>0){
			$qiniu_path=$this->getData("qiniu_path");
			foreach($list as $key=>$val){
				$list[$key]["file_url"]=null;
				$list[$key]["isme"]=($val["user_id"]==$user_id)?"1":"0";
				switch($list[$key]["content_type"]){
					case '0'://文字
						$text=new text();
						$content=$text->getTextById($list[$key]["content_id"]);
						$list[$key]["content"]=$content["content"];
						break;
					case '1'://音频
						$audio=new audio();
						$content=$audio->getAudioById($list[$key]["content_id"]);
						$content["name"] = str_replace("amr", "aac", $content["name"]);
						$list[$key]["content"] = $content['time'];
						$list[$key]["file_url"]=$qiniu_path["qiniu_path_audio"].$content["name"];
						break;
					case '2'://视频
						$video = new video();
						$content=$video->getVideoInfoById($video_id);
						$list[$key]["content"]= $content['time'];
						break;
					default://参数错误
						$data=null;
						$this->view->setMsg($video_id." para error!!!");
						$this->view->setState(0);
						$this->view->display("json");
						return;
						break;
				}
			}
			$totalCount = $qa->getQANumByVideo($video_id);
			$data=array("list"=>$list,"totalCount"=>count($list));
			//$this->view->setMsg($video_id."获取问答列表成功！");
			$this->view->setMsg("获取成功！");
			$this->view->setState(1);
		}else{
			$data=null;
			$this->view->setMsg("没有更多了哟");
			$this->view->setState(0);
		}
		$this->view->setData($data);
		$this->view->display("json");
	}
// 	系统反馈
// 	http://localhost/xdfServer/clientapi1.php/feedback/Sysfeeback/content/woshifankui
	public function Sysfeeback(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id = $userinfo ['id'];
 		$content=$this->getRequest()->get("content");
 		$phone=$this->getRequest()->get("phone");
 		$email=$this->getRequest()->get("email");
 		$phone=($phone)?$phone:"null";
 		$email=($email)?$email:"null";
 		$sysfeedback=new sysfeedback();
 		if($sysfeedback->addSysFeedback($user_id, $content,$phone,$email)){
			$this->view->setMsg("提交反馈成功！");
			$this->view->setState("1");
		}else{
			$this->view->setMsg("提交反馈失败！");
			$this->view->setState("0");
		}
		$this->view->setData(null);
		$this->view->display("json");
	}
	public function getSysFeedbackList(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id = $userinfo ['id'];
		$pagesize=$this->getRequest()->get("pagesize");
		$timestamp=$this->getRequest()->get("timestamp");
		$action=$this->getRequest()->get("action");
		$sysFeedback=new sysfeedback();
 		if($data["list"]=$sysFeedback->getSysFeedbackList($user_id,$timestamp,$pagesize,$action)){
 			$data["totalcount"]=count($data["list"]);
			$this->view->setMsg("获取反馈成功！");
			$this->view->setState("1");
		}else{
			$data=null;
			$this->view->setMsg("获取反馈失败！");
			$this->view->setState("0");
		}
		$this->view->setData($data);
		$this->view->display("json");
	}

	public function getSysFeedbackSingle(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id = $userinfo ['id'];
		$sysfeedback_id = $this->getRequest()->get('sysfeedback_id');
		$sysFeedback=new sysfeedback();
		$data=$sysFeedback->getMySysFeedback($user_id, $sysfeedback_id);
		if($data){
			$this->view->setMsg("获取反馈成功！");
			$this->view->setState("1");
		}else{
			$data=null;
			$this->view->setMsg("获取反馈失败！");
			$this->view->setState("0");
		}
		$this->view->setData($data);
		$this->view->display("json");
	
	}

	//检测新版本
	public function CheckVersion(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id = $userinfo ['id'];
		$user_role_id=$userinfo['role_id'];
		$data=null;
		//获取用户当前版本
		$current_version=$this->getRequest()->get("version");
		$type=$this->getRequest()->get("type");
		$version=new version();
		//检查并获取版本类型
		if($newVersion=$version->getLastVersionByType($type)){
			if(trim($current_version) === trim($newVersion["version"])){
				$this->view->setMsg("已经是最新版本！");
				$this->view->setState("0");
			}else{
				$data["version"]=$newVersion["version"];
				$data["desc"]=$newVersion["desc"];
				$data["url"]=$newVersion["url"];
				$data["release_time"]=$newVersion["release_time"];
				$this->view->setMsg("获取新版本成功！");
				$this->view->setState("1");
			}
		}else{
			$this->view->setMsg("获取失败！");
			$this->view->setState("0");
		}
		$this->view->setData($data);
		$this->view->display("json");
	}
}
